package com.jn.mvp.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jn.mvp.R;
import com.jn.mvp.base.BaseActivity;
import com.jn.mvp.contract.IdiomContract;
import com.jn.mvp.entity.Idiom;
import com.jn.mvp.presenter.IdiomPresenter;
import com.jn.mvp.utils.LogUtils;

public class IdiomActivity extends BaseActivity implements IdiomContract.IIdiomView, View.OnClickListener {

    private ImageView mImage;
    private EditText mEdit;
    private Button mButton;
    private Button mButton1;
    private Button mButton2;
    private TextView mText;
    private String mCurrentAnswer;

    private IdiomContract.IIdiomPresenter mPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_idiom;
    }

    @Override
    public void init() {
        mPresenter = new IdiomPresenter(this, this);
        mImage = findViewById(R.id.image);
        mEdit = findViewById(R.id.edit);
        mButton = findViewById(R.id.button);
        mButton1 = findViewById(R.id.button1);
        mButton2 = findViewById(R.id.button2);
        mButton.setOnClickListener(this);
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        mText = findViewById(R.id.text);
        mPresenter.getData();
    }

    @Override
    public void showData(Idiom data) {
        LogUtils.d("asdf", "===" + data.toString());
        Glide.with(this).load(data.getImg()).into(mImage);
        mCurrentAnswer = data.getAnswer();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            String answer = mEdit.getText().toString();
            if (!TextUtils.isEmpty(answer) && !TextUtils.isEmpty(mCurrentAnswer)) {
                if (answer.equals(mCurrentAnswer)) {
                    showToast("回答正确!");
                    mText.setText("");
                    if (mPresenter != null) {
                        mPresenter.getData();
                    }
                } else {
                    showToast("回答错误!");
                }
                mEdit.setText("");
            }
        } else if (id == R.id.button1) {
            if (mPresenter != null) {
                mPresenter.getData();
                mEdit.setText("");
                mText.setText("");
            }
        } else if (id == R.id.button2) {
            if (mCurrentAnswer != null) {
                mText.setText(mCurrentAnswer);
            }
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {

    }

    @Override
    public void onHttpFail(String... msg) {
        showToast(msg.length > 0 ? msg[0] : "");
    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }
}